home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3493 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.5 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Matrix Multiplication
  5. Date: Mon, 29 Jan 96 13:43:44 GMT
  6. Organization: none
  7. Message-ID: <822923024snz@genesis.demon.co.uk>
  8. References: <1996Jan22.110440@gamma.ntu.ac.sg> <4ecjv4$fn2@tamarack.cs.mtu.edu> <822792812snz@genesis.demon.co.uk> <DLwzv9.9wu@cwi.nl>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <DLwzv9.9wu@cwi.nl> dik@cwi.nl "Dik T. Winter" writes:
  15.  
  16. > > >Consider optimizing for cache performance.
  17. >[Omitting the initialization]
  18. > > >for ( i = 0; i < N; i++ )
  19. > > >   for ( j = 0; j < N; j++ )
  20. > > >      for ( k = 0; k < N; k++ )
  21. > > >         c[i][j] = c[i][j] + a[i][k]*b[k][j];
  22. > > 
  23. > > Or how about:
  24. > >       double sum = 0.0;
  25. >...
  26. > >       c[i][j] = sum;
  27. > > You compiler might be able to perform that optimisation for itself but
  28. > > aliasing issues may make the analysis difficult.
  29. >
  30. >In this case aliasing might make it difficult indeed or even impossible,
  31. >and doing it is indeed an improvement.  But the proposed version is *not*
  32. >optimal for caching; the optimal way to do it is to make the 'j' loop
  33. >innermost:
  34. >    for ( i = 0; i < N; i++)
  35. >       for (k = 0; k < N; k++)
  36. >          for (j = 0; j < N; j++)
  37. >             c[i][j] = c[i][j] + a[i][k]*b[k][j];
  38. >[and this is also optimal on vector processors].
  39. >The reason is that if at some stage c[i][j] (or b[k][j]) has a cache miss
  40. >a whole cache line is brought in, and this includes some elements of 'c' and
  41. >'b' that are needed on next steps through the loop.  Putting k innermost
  42. >gives the benefit only to 'a'.  (But it will also depend a lot on the
  43. >cache replacement strategy used by the processor.)
  44.  
  45. Right. Since in my version c is only ever written to cache line reads
  46. are not necessarily an issue. It is also outside the innermost loop so
  47. (in your case where N=1000) a cache miss compared to a 1000 iteration
  48. loop (with 1000 potential cache misses for b) should be insignificant.
  49. Indeed the chance of being able to accumulate the result in the inner loop
  50. in a register is likely to be more significant.
  51.  
  52. >Timings on an SGI with a 100 MHz MIPS R4k (in seconds with N = 1000, 10
  53. >multiplications):
  54. >i innermost             11.08
  55. >k innermost              5.92
  56. >k innermost + scalar     4.60
  57. >j innermost              2.59
  58.  
  59. I guess you'd need to analyse the object code to see what's going on there.
  60.  
  61. Still, this proves that where performance is critical there is no substitute
  62. for profiling the alternatives on the target system. It might be worth trying:
  63.  
  64.     /* zero all elements of c here */
  65.  
  66.     for (i = 0; i < N; i++) {
  67.        for (k = 0; k < N; k++) {
  68.           const double aik = a[i][k];
  69.  
  70.           for (j = 0; j < N; j++)
  71.              c[i][j] += aik * b[k][j];
  72.        }
  73.     }
  74.  
  75. since that is an optimisation the compiler definitely can't make for itself
  76. if it can't tell that a and c are distinct.
  77.  
  78. Actually this makes it clear why your version works fast - *everything* 
  79. in the inner loop caches well. The only disadvantage is that the c update
  80. requires an 'external' read-modify-write sequence. The performance of that
  81. will depend on how write caching is implemented.
  82.  
  83. Maybe C could do as well as FORTRAN in this case.
  84.  
  85. -- 
  86. -----------------------------------------
  87. Lawrence Kirby | fred@genesis.demon.co.uk
  88. Wilts, England | 70734.126@compuserve.com
  89. -----------------------------------------
  90.